package com.spboot.controller;



import com.spboot.entity.User;
import com.spboot.service.LikeService;
import com.spboot.service.ShoppingcartService;
import com.spboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guocb
 * @since 2020-04-27
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    LikeService likeService;

    @Autowired
    ShoppingcartService shoppingcartService;

    /**
     * 进登录页
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "login";
    }

    /**
     * 登录
     * @param phone
     * @param password
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/logina")
    public String login( Long phone, String password, HttpServletRequest request, Model model){
        User login=userService.login(phone, password);
        if (login!=null){
            request.getSession().setAttribute("login",login);
            return "/information/shouye";
        }else{

            return "/";
        }
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("logout")
    public String tologout(HttpServletRequest request){
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        return "/information/shouye";
    }

    /**
     * 用户页
     * @param model
     * @param userid
     * @return
     */
    @RequestMapping("suser")
    public  String user(Model model,Integer userid){
        if(userid!=null){
            model.addAttribute("user",userService.user(userid));
            model.addAttribute("sum",likeService.sum(userid));
            //右上购物车查询
            model.addAttribute("shoplist",shoppingcartService.ssum(userid));
            model.addAttribute("select",shoppingcartService.selectall(userid));
            model.addAttribute("shu",shoppingcartService.ssum(userid));
            model.addAttribute("zong",shoppingcartService.zong(userid));
            return "user";
        }else{
            model.addAttribute("user",userService.user(userid));
            model.addAttribute("sum",likeService.sum(userid));
            return "user";
        }
    }

    /**
     * 修改用户信息
     * @param username
     * @param phone
     * @param email
     * @param userid
     * @return
     */
    @RequestMapping("updateuser")
    public String updateuser(String username,String phone,String email,Integer userid){
        int user=userService.updateuser(username, phone, email, userid);
        System.out.println(user+"是否");
        if(user!=0){
            return "suser?userid="+userid;
        }
        return "";
    }
}

